<?php

namespace app\api\controller;

use app\api\model\GameCollect;
use app\api\model\GamePlayLog;
use app\api\model\User as UserModel;
use app\api\service\User as UserService;
use app\api\support\game\LightingPlay;
use app\api\model\GameModel;
use think\Request;
use think\Validate;

class Game
{
    public function lists(Request $request)
    {
        $page = $request->post('page', 1);
        $pageSize = $request->post('pageSize', 10);
        $where = [];
        $where['major_type'] = $request->post('major_type', 1);
        $list = GameModel::lists($where, $page, $pageSize);
        return json(['code' => 1, 'msg' => $list]);
    }

    public function play(Request $request)
    {
        $data = [];
        $data['gid'] = $request->post('gid');
        $user_id = UserService::getUserIdByToken();
        $user = UserModel::get($user_id);
        $data = [
            'openid' => $user_id,
            'nick' => 'mxq' . $user_id,
            'avatar' => $user['avatar'],
            'phone' => $user['mobile'],
        ];
        $extra = [
            'gid' => $data['gid']
        ];
        $url = LightingPlay::redirectUrl($data, $extra);
        return $url;
    }

    /**
     * @api {post} /api/game/collectList 收藏列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\api\exception\BaseException
     */
    public function collectList(Request $request)
    {
        $user_id = UserService::getUserIdByToken();
        $page = $request->post('page', 1);
        $pageSize = $request->post('pageSize', 10);
        $where = [];
        $where['user_id'] = $user_id;
        $list = GameCollect::lists($where, $page, $pageSize);
        $gameIdArr = array_column($list, 'game_id');
        $gameList = GameModel::all(['id' => $gameIdArr]);
        foreach ($list as $k => $v) {
            foreach ($gameList as $v2) {
                if ($v['game_id'] == $v2['id']) {
                    $list[$k]['gameInfo'] = $v2;
                }
            }
        }
        return json(['code' => 1, 'msg' => $list]);
    }

    /**
     * @api {post} /api/game/collect 收藏添加
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\api\exception\BaseException
     */
    public function collect(Request $request)
    {
        $user_id = UserService::getUserIdByToken();
        $game_id = $request->post('game_id');
        $data = [];
        $data['user_id'] = $user_id;
        $data['game_id'] = $game_id;
        $res = GameCollect::insert($data);
        $output = $res ? ['code' => 1, 'msg' => '收藏成功'] : ['code' => 0, 'msg' => '收藏失败,请重试'];
        return json($output);
    }

    /**
     * @api {post} /api/game/playtime 游戏时间记录
     *
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\api\exception\BaseException
     */
    public function playTime(Request $request)
    {
        $user_id = UserService::getUserIdByToken();
        $begin = $request->post('begin');
        $end = $request->post('end');
        $gid = $request->post('gid');
        $validate = new Validate([
            'begin' => 'require',
            'end' => 'require',
            'gid' => 'require'
        ]);
        $data = [];
        $data['begin'] = $begin;
        $data['end'] = $end;
        $data['gid'] = $gid;
        if (!$validate->check($data)) {
            return json(['code' => 0, 'msg' => $validate->getError()]);
        }
        $data['user_id'] = $user_id;
        $res = GamePlayLog::insert($data);
        $output = $res ? ['code' => 1, 'msg' => '记录成功'] : ['code' => 0, 'msg' => '记录失败,请重试'];
        return json($output);
    }
}